﻿using System;
using StructureInheritance.Model;
using Xtensive.Orm;
using Xtensive.Orm.Configuration;

namespace StructureInheritance
{
    public class Program
    {
        public static void Main()
        {
            // Loading domain configuration section with name "Default".
            var domainConfiguration = DomainConfiguration.Load("Default");
            var domain = Domain.Build(domainConfiguration);

            using (var session = domain.OpenSession()) {
                using (var tx = session.OpenTransaction()) {

                    // Creating 2 points for a Range
                    var start = new Point2D(session) {
                        X = 0,
                        Y = 0
                    };
                    var end = new Point2D(session) {
                        X = 10,
                        Y = 20
                    };

                    // Creating a Range instance
                    var range = new Range(session) {
                        Start = start,
                        End = end
                    };

                    // Now, if we have a Point3D instance, we can try assigning it to Range.Start property
                    var start3D = new Point3D(session) {
                        X = 1,
                        Y = 1,
                        Z = 1
                    };

                    try {
                        range.Start = start3D;
                    } catch (InvalidOperationException ex) {
                        Console.WriteLine(ex.Message);
                    }

                    // You must make the assignment constructing a new instance of Point2D
                    range.Start = new Point2D(session) {
                        X = start3D.X,
                        Y = start3D.Y
                    };
                    // Or assign fields one by one
                    range.Start.X = start3D.X;
                    range.Start.Y = start3D.Y;

                    tx.Complete();
                }
            }
        }
    }
}